package com.liy.supercloud.ssoserver;

import com.liy.framework.common.annotation.IgnoreUserToken;
import com.liy.framework.common.extvo.ResultVO;
import com.liy.framework.common.utils.DateUtil;
import com.liy.framework.common.webmsg.WebMessage;
import com.liy.supercloud.ssoserver.feign.SystemFeignClient;
import com.liy.supercloud.ssoserver.feign.WebMessageClient;
import com.liy.supercloud.ssoserver.vo.SysLoginLog;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@RestController
@EnableScheduling
@EnableFeignClients( basePackages = {"com.das.supercloud.ssoserver.feign"} )
@MapperScan({"com.das.cloud.system.dao"})
@ComponentScan({"com.liy.cloud.system"})
@EnableEurekaClient
public class AuthApplication {

    @Autowired
    public WebMessageClient webMessageClient;

    @Autowired
    private SystemFeignClient systemFeignClient;


    public static void main(String[] args) {
        SpringApplication.run(AuthApplication.class, args);
    }

    @IgnoreUserToken
    @RequestMapping({"/"})
    public String index() {
        return "OSC Auth server....";
    }

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

    @IgnoreUserToken
    @RequestMapping({"/login/test"})
    public ResultVO login() throws Exception {
        SysLoginLog loginLog = new SysLoginLog();
        loginLog.setAccount("abad");
        loginLog.setPassword("2323");
        loginLog.setLogindate(DateUtil.getTimeNow());
        loginLog.setLoginClient("dfdf");
        loginLog.setLoginip("dfdf");
        return this.systemFeignClient.addLoginLog(loginLog);
    }


    @IgnoreUserToken
    @RequestMapping({"/webmessage/test"})
    public ResultVO webMessageClientTest() throws Exception {
        WebMessage message = new WebMessage("aaaa", "ddddd", "00");
        return this.webMessageClient.sendClient(message, "abc");
    }


    @RequestMapping({"/api/test"})
    public byte[] test() throws NoSuchAlgorithmException {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        SecureRandom secureRandom = new SecureRandom("ABCD".getBytes());
        keyPairGenerator.initialize(1024, secureRandom);
        KeyPair keyPair = keyPairGenerator.genKeyPair();
        PublicKey pk = keyPair.getPublic();
        System.out.println(keyPair.getPublic().getFormat());
        byte[] publicKeyBytes = keyPair.getPublic().getEncoded();
        byte[] privateKeyBytes = keyPair.getPrivate().getEncoded();
        Map<String, byte[]> map = new HashMap();
        map.put("pub", publicKeyBytes);
        map.put("pri", privateKeyBytes);
        return publicKeyBytes;
    }
}
